package com.yiren.dbaa.util;

import java.text.DecimalFormat;

/**
 * Describe 数字转换的工具类
 */
public class DecimalTransferUtil {

    /**
     * 将byte流量转换为大流量单位
     *
     * @param size
     * @return
     */
    public static String readableFileSize(double size) {
        if (size <= 0) return "0";
        final String[] units = new String[]{"B", "KB", "MB", "GB", "TB"};
        int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
        return new DecimalFormat("#,##0.##").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
    }

    /**
     * 将米转换成KM并保留三位小数
     *
     * @param meter
     * @return
     */
    public static String readableMeter(double meter) {
        if (meter <= 0) {
            return "0";
        }
        return new DecimalFormat("#,##0.###").format(meter / 1000.0);
    }

    /**
     * 将米格式化
     * @param meter
     * @return
     */
    public static String formatMeter(double meter) {
        if (meter <= 0) {
            return "0";
        }
        return new DecimalFormat("#,##0.##").format(meter);
    }
}
